package fr.cephb.joperon.webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;


public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Resource(name="jdbc/OperonDS")
private DataSource datasource;

@PersistenceContext(name="TopLinkPersistenceUnit2")
private EntityManager topLink;

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException
	{
	resp.setContentType("text/html");
	PrintWriter out=resp.getWriter();
	out.println("<html><body>");
	out.println("<h1>Test Servlet</h1>");
	out.println("req.getPathInfo:"+req.getPathInfo()+"<br/>");
	out.println("req.getServerName:"+req.getServerName()+"<br/>");
	out.println("req.getContextPath:"+req.getContextPath()+"<br/>");
	out.println("req.getLocalName:"+req.getLocalName()+"<br/>");
	out.println("req.getContentType:"+req.getContentType()+"<br/>");
	out.println("req.getMethod:"+req.getMethod()+"<br/>");
	out.println("req.getPathTranslated:"+req.getPathTranslated()+"<br/>");
	out.println("req.getRequestURL:"+req.getRequestURL()+"<br/>");
	out.println("req.getProtocol:"+req.getProtocol()+"<br/>");
	out.println("req.getQueryString:"+req.getQueryString()+"<br/>");
	out.println("req.getRemoteHost:"+req.getRemoteHost()+"<br/>");
	out.println("req.getRemotePort:"+req.getRemotePort()+"<br/>");
	out.println("req.getRemoteUser:"+req.getRemoteUser()+"<br/>");
	out.println("req.getRequestURI:"+req.getRequestURI()+"<br/>");
	out.println("req.getScheme:"+req.getScheme()+"<br/>");
	HttpSession session=req.getSession();
	if(session==null)
		{
		out.println("Cannot get session<br/>");
		}
	else
		{
		out.println("session.isNew()="+session.isNew()+"<br/>");
		out.println("session.getId()="+session.getId()+"<br/>");
		Object o=session.getAttribute(JOperonConstants.getUserSession());
		if(o==null)
			{
			out.println("session: no userBean<br/>");
			}
		else
			{
			out.println("session:userBean "+ o.getClass()+"<br/>");
			}

		}
	
	if(this.datasource==null)
		{
		out.println("session: datasource is null<br/>");
		}
	else
		{
		
		Connection con=null;
		try {
		con= datasource.getConnection();
		
		out.print("OK got a connection to derby<br/>");	
		Statement stmt= con.createStatement();
		ResultSet row=stmt.executeQuery("select count(*) from operon.test");
		while(row.next())
			{
			out.print("ResultSet:"+row.getInt(1)+"<br/>");	
			}
		
		} catch (Exception e) {
			e.printStackTrace(out);
		out.print("Error:"+e.getMessage()+"<br/>");	
		
		}
		finally
		{
			if(con!=null) try{ con.close();} catch(Throwable err) { }
		}
		}
	
	/** test toplink */
	try {
		Query q=topLink.createQuery("SELECT p FROM User p WHERE p.id > 0");
		for(Object o: q.getResultList())
			{
			out.print(o+"</br>");	
			}
	} catch (Exception e) {
		out.print("<pre>");
		e.printStackTrace(out);
		out.print("</pre>");
		out.print("Error:"+e.getMessage()+"<br/>");	
	}
	
	out.println("</body></htm>");
	
	out.flush();

	}


}
